import datetime
import logging
import sys
import time

from PyQt5 import QtWidgets
from PyQt5.QtCore import QDate, QDateTime, QTime
from PyQt5.QtWidgets import QWidget

from data.DataHandler import datahandler
from gcs_views.ui_takeoff_checklist import Ui_takeoff_checklist


def check_proc(check_box: QtWidgets.QCheckBox):
    if check_box.checkState():
        check_box.setStyleSheet('color: rgb(0, 255, 0);font: 75 12pt "Noto Sans S Chinese Bold";')
        return 0
    else:
        check_box.setStyleSheet('color: rgb(255, 255, 255);font: 75 12pt "Noto Sans S Chinese Bold";')
        return 1


def check_proc_ratio(check_box: QtWidgets.QRadioButton):
    if check_box.isChecked():
        check_box.setStyleSheet('color: rgb(0, 255, 0);font: 75 12pt "Noto Sans S Chinese Bold";')
        return 0
    else:
        check_box.setStyleSheet('color: rgb(255, 255, 255);font: 75 12pt "Noto Sans S Chinese Bold";')
        return 1


class TakeoffCheckListPad(QWidget, Ui_takeoff_checklist):
    def __init__(self):
        super().__init__()
        # self.setWindowTitle("子窗口")
        self.setupUi(self)
        self.gui_init()

    def gui_init(self):
        # 初始化日期时间和起飞时间
        self.edit_date.setDate(QDate.currentDate())
        self.edit_takeoff_time.setTime(QTime.currentTime())

        # 注册检查按钮事件
        self.check_plane_1.clicked.connect(self.takeoff_check)
        self.check_plane_2.clicked.connect(self.takeoff_check)
        self.check_plane_3.clicked.connect(self.takeoff_check)
        self.check_plane_4.clicked.connect(self.takeoff_check)
        self.check_plane_5.clicked.connect(self.takeoff_check)

        self.radioButton.clicked.connect(self.takeoff_check)
        self.radioButton_1.clicked.connect(self.takeoff_check)
        self.check_config_1.clicked.connect(self.takeoff_check)
        self.check_config_2.clicked.connect(self.takeoff_check)
        self.check_engine_1.clicked.connect(self.takeoff_check)
        self.check_engine_2.clicked.connect(self.takeoff_check)
        self.check_act_1.clicked.connect(self.takeoff_check)
        self.check_act_2.clicked.connect(self.takeoff_check)
        self.check_act_3.clicked.connect(self.takeoff_check)
        self.check_act_4.clicked.connect(self.takeoff_check)
        self.check_act_5.clicked.connect(self.takeoff_check)
        self.check_act_6.clicked.connect(self.takeoff_check)
        self.check_act_7.clicked.connect(self.takeoff_check)
        self.check_sensor_1.clicked.connect(self.takeoff_check)
        self.check_sensor_2.clicked.connect(self.takeoff_check)
        self.check_sensor_3.clicked.connect(self.takeoff_check)
        self.check_sensor_4.clicked.connect(self.takeoff_check)
        self.check_sensor_5.clicked.connect(self.takeoff_check)
        self.check_sensor_6.clicked.connect(self.takeoff_check)
        self.check_device_1.clicked.connect(self.takeoff_check)
        self.check_device_2.clicked.connect(self.takeoff_check)
        self.check_device_3.clicked.connect(self.takeoff_check)
        self.btn_check_complete.clicked.connect(self.takeoff_check_complete)

    def takeoff_check_complete(self):
        # 保存起飞前检查数据
        filename = time.strftime('takeoff_check_report_%Y%m%d_%H%M%S.log', time.localtime(time.time()))
        try:
            with open(filename, 'w') as f:
                date_str = self.edit_date.text()
                f.write("\n记录日期：" + date_str)
                f.write("\n起始机场：" + self.edit_orig_apt.text())
                f.write("\n目的机场：" + self.edit_dest_apt.text())
                f.write("\n当前天气：" + self.edit_weather.currentText())
                f.write("\n风速：" + self.edit_wind_spd.text()+"m/s")
                f.write("\n风向：" + self.edit_wind_direction.text() + "°")
                f.write("\n起飞时间：" + self.edit_takeoff_time.text())
                f.write("\n气象报文：" + self.edit_weather_broadcast.toPlainText())
                f.write("\n试车最大转速：" + self.edit_max_rpm.text() + "rpm")
                f.write("\nA路掉转转速：" + self.edit_testA.text() + "rpm")
                f.write("\nB路掉转转速：" + self.edit_testB.text() + "rpm")
                f.write("\n完成检查时间：" + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
                f.close()
        except Exception as e:
            logging.error(repr(e))
        # 关闭起飞检查单页面
        self.close()
        logging.info("完成起飞前检查")
        datahandler.takeoff_check_finished = True

    def takeoff_check(self):
        check_fail = 0
        check_fail += check_proc(self.check_plane_1)
        check_fail += check_proc(self.check_plane_2)
        check_fail += check_proc(self.check_plane_3)
        check_fail += check_proc(self.check_plane_4)
        check_fail += check_proc(self.check_plane_5)

        tmp = check_proc_ratio(self.radioButton) + check_proc_ratio(self.radioButton_1)
        if tmp == 2:
            check_fail += 1

        check_fail += check_proc(self.check_config_1)
        check_fail += check_proc(self.check_config_2)

        check_fail += check_proc(self.check_engine_1)
        check_fail += check_proc(self.check_engine_2)

        check_fail += check_proc(self.check_act_1)
        check_fail += check_proc(self.check_act_2)
        check_fail += check_proc(self.check_act_3)
        check_fail += check_proc(self.check_act_4)
        check_fail += check_proc(self.check_act_5)
        check_fail += check_proc(self.check_act_6)
        check_fail += check_proc(self.check_act_7)

        check_fail += check_proc(self.check_sensor_1)
        check_fail += check_proc(self.check_sensor_2)
        check_fail += check_proc(self.check_sensor_3)
        check_fail += check_proc(self.check_sensor_4)
        check_fail += check_proc(self.check_sensor_5)
        check_fail += check_proc(self.check_sensor_6)

        check_fail += check_proc(self.check_device_1)
        check_fail += check_proc(self.check_device_2)
        check_fail += check_proc(self.check_device_3)

        if check_fail > 0:
            self.btn_check_complete.setEnabled(False)
            self.btn_check_complete.setStyleSheet('background-color: rgb(255, 0, 0);color: rgb(0, 0, 0);font: 75 '
                                                  '12pt "Noto Sans S Chinese Bold";')
        else:
            self.btn_check_complete.setEnabled(True)
            self.btn_check_complete.setStyleSheet('background-color: rgb(0, 255, 0);color: rgb(0, 0, 0);font: 75 '
                                                  '12pt "Noto Sans S Chinese Bold";')


# 地面站软件的入口程序
if __name__ == '__main__':
    # 启动地面站主界面
    app = QtWidgets.QApplication(sys.argv)
    gcs_main_window = QtWidgets.QMainWindow()
    ui = TakeoffCheckListPad()
    ui.setupUi(gcs_main_window)
    ui.gui_init()
    gcs_main_window.show()  # 显示窗口

    sys.exit(app.exec_())
